package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class MainMenu {

    List<IBorrowObject> books = new ArrayList<IBorrowObject>();
    List<IBorrowObject> movies = new ArrayList<IBorrowObject>();
    ListBorrowObject listBooks = new ListBorrowObject();
    ListBorrowObject listMovies = new ListBorrowObject();
    Scanner scanner;

    public MainMenu(List<IBorrowObject> books, List<IBorrowObject> movies) {
        this.books = books;
        this.movies = movies;

    }



    public void mainMenuOption(){
        System.out.println("****************************************************************************************************************************");
        listBooks.loadListBorrowObject("List Books", books);

        System.out.println();

        System.out.println("Main menu           List Books          Quit");

        System.out.println("****************************************************************************************************************************");
        scanner = new Scanner(System.in);
        String optionName = scanner.nextLine();
        if(optionName.equals("Quit")){
            return ;
        } else if (optionName.equals("List Books")){
            listOption(optionName,"book",books,listMovies);

        } else if(optionName.equals("List Movies")){
            listOption(optionName, "movie", movies, listMovies);

        }else{
            System.out.println("Select a valid option!");
            mainMenuOption();
        }
    }

    private void listOption(String optionName, String borrowType, List<IBorrowObject> borrowObject, ListBorrowObject listBorrowObject) {
        listBorrowObject.loadListBorrowObject(optionName,borrowObject);
        System.out.println();
        System.out.println("if you want goto the main menu,please enter 'goBack'");
        System.out.println("if you want check out "+ borrowType +",please enter 'check out'");
        System.out.println("if you want return  "+borrowType+",please enter 'return'");
        String input = scanner.nextLine();

        if(input.equals("Quit")){
            return ;
        }else if (input.equals("check out")){
            System.out.println("please input the book id you want borrow");
            String checkOutBookResult = CheckOutBook.getCheck(this, borrowObject,scanner.nextLine(),borrowType);
            System.out.println(checkOutBookResult);
            listOption(optionName,borrowType,borrowObject,listBorrowObject);
        } else if(input.equals("return")){
            System.out.println("please input the book id you want return");
            String returnBookResult= ReturnBook.getReturn(this, borrowObject, scanner.nextLine(),borrowType);
            System.out.println(returnBookResult);
            listOption(optionName,borrowType,borrowObject,listBorrowObject);
        } else if (input.equals("goBack")){
            mainMenuOption();
        } else {
            listOption(optionName,borrowType,borrowObject,listBorrowObject);
        }

    }

}